﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace HttpServer
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Socket sServer=new Socket(
                AddressFamily.InterNetwork,SocketType.Stream ,
                ProtocolType.Tcp 
                );
            sServer.Bind(new IPEndPoint(IPAddress.Parse(textBox1.Text),
                int.Parse(textBox2.Text)));
            sServer.Listen(100);

            Thread taccept=new Thread(() =>
            {
                while (true)
                {
                 Socket sClient=sServer.Accept();
                    byte[] bs=new byte[1024];
                   int len= sClient.Receive(bs);
                    HttpContent content=new HttpContent(bs,len);
                    if (len<=0)
                    {
                        continue;
                    }
                    HttpApplication app=new HttpApplication();
                    app.ProcessRequest(content,sClient);
                }
            });
            taccept.IsBackground = true;
            taccept.Start();
            button1.Text = "服务器已启动";
        }
    }
}
